﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VerifySystem
{
    /// <summary>
    /// 卡号验证接口
    /// </summary>
    public interface ICardVerify
    {
        bool ValidateCard(VerifyParam param);
    }
    /// <summary>
    /// 银联卡号验证类
    /// </summary>
    public class YinlianCardVerify : ICardVerify
    {
        public bool ValidateCard(VerifyParam param)
        {
            return true;
        }
    }
    /// <summary>
    /// Paypal卡号验证类
    /// </summary>
    public class PaypalCardVerify : ICardVerify
    {
        public bool ValidateCard(VerifyParam param)
        {
            return false;
        }
    }

    public class VerifyMgr
    {
        /// <summary>
        /// 卡号验证类工厂
        /// </summary>
        private Dictionary<string, Func<ICardVerify>> _factories;

        public VerifyMgr()
        {
            _factories = new Dictionary<string, Func<ICardVerify>> {
                { InType.Yinlian,()=>new YinlianCardVerify()},
                { InType.Paypal,()=>new PaypalCardVerify()}
            };
        }
        /// <summary>
        /// 获取卡号验证类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public ICardVerify GetCardVerify(string type)
        {
            if (_factories.ContainsKey(type))
            {
                var factories = _factories[type];
                return factories();
            }
            else
            {
                throw new ArgumentException("Invalid type", nameof(type));
            }
        }
        /// <summary>
        /// 调用卡号验证类的验证方法
        /// </summary>
        /// <param name="cardVerify"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool CardVerify(ICardVerify cardVerify, VerifyParam param)
        {
            return cardVerify.ValidateCard(param);
        }
    }

    public class VerifyParam
    {
        /// <summary>
        /// 银行卡号
        /// </summary>
        public string CardNo { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        public string PhoneNo { get; set; }

        /// <summary>
        /// 身份证号
        /// </summary>
        public string IdNo { get; set; }
    }

    public static class InType
    {
        public const string Yinlian = "A";

        public const string Paypal = "B";
    }
}
